import re, math
import os
import sys
from collections import Counter
sys.path.insert(0, os.path.abspath('..'))
from scorer.scorer import Scorer

class Summarizer:
    """Summarizes the text to given length"""

    def __init__(self):
        self.__originalText = ""
        self.__lengthOfSummary = ""
        self.__summary = ""

    def inputTextAndLength(self, originalText, lengthOfSummary):
        """Input the text and length for summarization"""

        self.__originalText = originalText
        self.__lengthOfSummary = lengthOfSummary

    
    def summarize(self):
        """Summarizes the text"""
                
        sentences = re.split(r' *[\.\?!][\'"\)\]]* *', self.__originalText)
        sentences.pop()
        lenOfSentences = len(sentences)
        iterations = lenOfSentences - self.__lengthOfSummary
        candidateSummary = sentences[:]
        for i in range(iterations):
            maxSimilarityScore = 0
            tempCandidateSummary = []
            for j in range(lenOfSentences):
                tempSentences = candidateSummary[:]
                tempSentences.pop(j)
                tempText = ". ".join(tempSentences)
                getScore = Scorer()
                currentSimilarityScore = getScore.scorer(tempText, self.__originalText)
                print tempText,"\n",getScore.scorer(tempText, self.__originalText)
                if currentSimilarityScore > maxSimilarityScore:
                    self.__summary = tempText
                    tempCandidateSummary = tempSentences[:]
                    maxSimilarityScore = currentSimilarityScore
            candidateSummary = tempCandidateSummary[:]
            lenOfSentences -= 1
        self.__summary += '.'
	
    def getSummary(self):
        """returns generated summary"""

        return self.__summary

    def getOriginalText(self):
        """return text entered for summarization"""
        
        return self.__originalText

if __name__=="__main__":
    originalText = raw_input("Enter a string: ")
    lengthOfText = input("Enter length of summary: ")    
    summarizer = Summarizer()
    summarizer.inputTextAndLength(originalText, lengthOfText)
    summarizer.summarize()
    print "\n\nOriginal Text:: ", summarizer.getOriginalText() , "\n"
    print "Summary:: ", summarizer.getSummary() , "\n"
